<template>
    <div>
        <div class="ranch-live-box">
            <div class="live-ranch-name">{{detailData.farmTitle}}</div>
            <div class="live-ranch-bg">
                <div class="player">
                    <!-- 视频 -->
                    <video
                        controls="controls"
                        x5-playsinline="true"
                        playsinline="true"
                        webkit-playsinline="true"
                        preload="auto"
                        poster="https://ym-1251385011.cos.ap-guangzhou.myqcloud.com/images/20191010/15707106801007203.png"
                        v-if="liveType == 2">
                        <source src="http://ym-1251385011.cos.ap-guangzhou.myqcloud.com/videos/farmvideo.mp4" type="video/mp4">
                    </video>
                    
                    <!-- 直播 -->
                    <video-player 
                        class="video-player vjs-custom-skin"
                        ref="videoPlayer"
                        x5-video-player-type="h5"
                        x5-playsinline="true"
                        :playsinline="playsinline"
                        :options="playerOptions"
                        @canplay="onPlayerCanplay($event)"
                        v-else>
                    </video-player>
                </div>
            </div>
            <div class="live-ranch-intro">
                <p>牧场介绍</p>
                <p>{{detailData.farmDescription}}</p>
            </div>
        </div>
    </div>
</template>

<script>
import {videoPlayer} from 'vue-video-player'
require('video.js/dist/video-js.css')
require('vue-video-player/src/custom-theme.css')
import 'videojs-contrib-hls'

export default {
    props: ['liveType'],
    components: {
        videoPlayer
    },
    data() {
        return {
            // playsinline: '',
            detailData: {
                farmTitle: '直播牧场位于甘肃旦马牧场',
                farmLivePic: 'https://ym-1251385011.cos.ap-guangzhou.myqcloud.com/images/20191010/15707106801007203.png',
                farmDescription: "旦马牧场地处北纬35º-40º，原生态世界黄金肉牛生产带。旦马草原，纯天然的优质牧场—距牧场40公里。每年会将牛只赶放至旦马草原进行天然放养，结束放牧期后回牧场进行统一的谷饲育肥。牧场实时监管牛只养殖情况，保障牛只养殖过程安全可控。用户可在平台查看视频直播，查看牛群的管理情况和成长状态。"
            },
            playerOptions: {
                // playbackRates: [0.7, 1.0, 1.5, 2.0], //播放速度
                autoplay: false, //如果true,浏览器准备好时开始回放。
                controls: false, //显示控制栏
                muted: false, // 默认情况下将会消除任何音频。
                loop: false, // 导致视频一结束就重新开始。
                preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
                language: 'zh-CN',
                aspectRatio: '16:9', // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
                fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
                sources: [{
                    type: "application/x-mpegURL",
                    src: "http://hls01open.ys7.com/openlive/74149641a2b640e8b0e2fed3dff90edb.m3u8" //视频直播地址（必填）
                }],
                poster: "https://ym-1251385011.cos.ap-guangzhou.myqcloud.com/images/20191010/15707106801007203.png", //你的封面地址
                width: document.documentElement.clientWidth,
                notSupportedMessage: '此视频暂无法播放，请稍后再试', //允许覆盖Video.js无法播放媒体源时显示的默认信息。
            }
        }
    },
    created() {
        // this.playsinline = this.playsinline()
        // console.log('playsinline:',this.playsinline)
    },
    computed: {
        playsinline(){
            var ua = navigator.userAgent.toLocaleLowerCase();
            //x5内核
            if (ua.match(/tencenttraveler/) != null || ua.match(/qqbrowse/) != null) {
                return false
            }else{
                //ios端
                return true				
            }
        }
    },
    methods: {
        onPlayerCanplay(player) {
            // console.log('player Canplay!', player)
            //解决自动全屏
            var ua = navigator.userAgent.toLocaleLowerCase();
            if (ua.match(/tencenttraveler/) != null || ua.match(/qqbrowse/) != null) {
                //x5内核
                $('body').find('video').attr('x-webkit-airplay',true).attr('x5-playsinline',true).attr('webkit-playsinline',true).attr('playsinline',true)
            }else{
                //ios端
                $('body').find('video').attr('webkit-playsinline',"true").attr('playsinline',"true") 
            }
        }
    }
}
</script>

<style lang="scss" scoped>
    .ranch-live-box {
		width: 100%;
		padding: 24px 16px;
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
		.live-ranch-name {
			width: 278px;
			height: 42px;
			margin: 0 auto 16px;
			line-height: 42px;
			text-align: center;
			background: url('../../assets/img/ranch_live_bg.png') center no-repeat;
			background-size: cover;
			font-size: 16px;
			color: #fff;
		}
		.live-ranch-bg {
			width: 100%;
			height: 183px;
			margin-bottom: 24px;
			background-repeat: no-repeat;
            background-position: center;
			background-size: cover;
            video {
                width: 100%;
            }
		}
		.live-ranch-intro {
			p {
				font-size: 12px;
				color: #969C9E;
				text-indent: 2em;
				&:nth-child(1) {
					margin-bottom: 10px;
					font-size: 14px;
					color: #5D6164;
					text-indent: 0;
				}
			}
		}
	}
</style>
<style>
    .video-js .vjs-big-play-button {
        width: 2em;
        border-radius: 50%;
        border: 2px solid #ccc;
    }
    .vjs-custom-skin > .video-js .vjs-big-play-button {
        margin-left: -1em;
    }
    .video-js:hover .vjs-big-play-button, .vjs-custom-skin > .video-js .vjs-big-play-button:focus, .vjs-custom-skin > .video-js .vjs-big-play-button:active {
        background-color: rgba(0, 0, 0, 0);
    }
    .vjs-custom-skin > .video-js .vjs-loading-spinner {
        border-color: rgba(49, 188, 141, .9)
    }
</style>